package com.yolo.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yolo.entity.Emp;
import com.yolo.entity.Errand;
import com.yolo.entity.Post;
import com.yolo.entity.Salary;
import com.yolo.service.EmpService;
import com.yolo.service.ErrandService;
import com.yolo.service.PostService;
import com.yolo.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author:天蝎
 * @Date:2021/4/26 16:37
 * @Version 1.0
 */
@Component
public class SalaryUtil {
    @Autowired
    private SalaryService salaryService;
    @Autowired
    private ErrandService errandService;
    @Autowired
    private EmpService empService;
    @Autowired
    private PostService postService;

    public void saveSalary() {
        List<Integer> state = new ArrayList<>();
        state.add(0);
        state.add(1);
        //基本工资数据
        List<Emp> list = empService.list(new QueryWrapper<Emp>().in("state", state));
        Map<Integer, List<Double>> map = new HashMap<>();
        for (Emp e : list) {
            //岗位Id
            String[] split = e.getPostIds().split(",");
            List<Post> posts = postService.list(new QueryWrapper<Post>().in("post_id", split));
            List<Double> doubles = new ArrayList<>();
            for (Post p : posts) {
                doubles.add(p.getSalary());
            }
            map.put(e.getEmpId(), doubles);
        }
        Map<Integer, Double> map1 = new HashMap<>();
        map.forEach((key, value) -> {
            Double max = Collections.max(value);
            Double sum = 0.0;
            for (Double d : value) {
                sum += d;
            }
            if (value.size() > 1) {
                sum = (sum - max) / 2 + max;
            }
            map1.put(key, sum);
        });
        map1.forEach((key, value) -> {
            Salary salary = new Salary();
            salary.setEmpId(key);
            LocalDateTime now = LocalDateTime.now();
            salary.setGrantTime(now);
            salary.setMonth(now.format(DateTimeFormatter.ofPattern("YYYY-MM")));
            salary.setBaseSalary(value);
            //根据员工编号查询员工出差信息
            List<Errand> errands = errandService.queryErrandByCurrentMonth(key);
            Double errandSalary = 0.0;
            if (errands != null) {
                for (Errand e : errands) {
                    Integer errandDay = e.getErrandDay();
                    Double errandSubsidy = e.getErrandSubsidy();
                    double v = errandDay * errandSubsidy;
                    errandSalary += v;
                }
            }
            salary.setErrandSubsidys(errandSalary);
            double fullAttendance = 200.00;
            salary.setFullAttendance(fullAttendance);
            salary.setWorkDate(maxDate());
            double fine = 0.0;
            salary.setFine(fine);
            double price = value + errandSalary + fullAttendance - fine;
            salary.setSb(price * 0.13);
            double jb = price - salary.getSb();
            double tax = 0.0;
            if (jb > 5000) {
                double s = jb - 5000;
                if (jb >= 5001 && jb <= 8000) {
                    tax = s * 0.03;
                } else if (jb >= 8001 && jb <= 17000) {
                    tax = s * 0.10;
                } else if (jb >= 17001 && jb <= 30000) {
                    tax = s * 0.20;
                } else if (jb >= 30001 && jb <= 40000) {
                    tax = s * 0.25;
                } else if (jb >= 40001 && jb <= 60000) {
                    tax = s * 0.30;
                } else if (jb >= 60001 && jb <= 85000) {
                    tax = s * 0.35;
                } else {
                    tax = s * 0.4;
                }
            }
            salary.setTax(tax);
            salary.setFinalSalary(jb - tax);
            Emp byId = empService.getById(key);
            if (byId.getState() == 0) {
                salary.setFinalSalary((jb - tax) * 0.75);
            }
            salaryService.save(salary);
        });
    }

    public static int maxDate() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
}
